<?php

/**
 * File Name：PublicController.php
 * File Encoding：UTF-8
 * File New Time：2014-8-5 11:22:39
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class PublicController extends YzwController {

    //发送短信验证码
    public function actionSnscode() {
        $moblie = Yii::app()->request->getParam('moblie');
        if (empty($moblie)) {
            $this->error('手机号不能为空！');
        }
        $key = md5(GuestDataModel::model()->getForwardedForIp());
        if (Yii::app()->cache->get($key)) {
            $this->error('请60秒后再次发送！');
        }
        if (SnsVerification::getInstance()->send($moblie, 'cellphone')) {
            Yii::app()->cache->set($key, 1, 60);
            $this->success('短信验证码已经发送！');
        } else {
            $this->error('验证码发送失败！');
        }
    }
    //分享商品增加积分
    public function actionShare() {

        $goods_id = Yii::app()->request->getParam('goods_id');
        if(!yii::app()->user->isLogged()){
           $this->error('用户未登录'); 
        }
        $uid = Yii::app()->user->id;
        $user = MembersModel::model()->findByPk($uid);
        if (empty($user)) {
            $this->error('获取用户信息失败！', self::U('user/login/login'));
        }
        $objGoodsModel = GoodsModel::model()->findByPk($goods_id);
        if (!$objGoodsModel) {
            $this->error('商品信息错误');
        }
        $key = "RedeemgoodController_Share_{$goods_id}_{$uid}";
        $key_count = "RedeemgoodController_Share_count_{$uid}";//记录此用户今天分享商品的次数，超过30次则不再送积分
        $cache = Yii::app()->cache->get($key);
        $time = time();
        $time_today = strtotime('today');

        if(empty($cache)){
            Yii::app()->cache->set($key, $time, $time_today + 86399 - $time);
        }else{
            if($cache > $time_today){ //今天分享过
                $this->error('今天已经分享过此商品');
            }else{//今天没有分享过
                Yii::app()->cache->set($key, $time, $time_today + 86399 - $time);
            }
        }

        $cache_count = Yii::app()->cache->get($key_count);
        if(empty($cache_count)){
            Yii::app()->cache->set($key_count, 1, $time_today + 86399 - $time);
        }else{
            if($cache_count >= 30){
                $this->error('今天分享的商品已经达到30个');
            }else{
                Yii::app()->cache->set($key_count, $cache_count+1, $time_today + 86399 - $time);
            }
        }

        //增加积分
        if (MembersModel::model()->plusScore($uid, 5) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享送积分活动，赠送积分 5 。');
            //经验值
            MembersModel::model()->plusExp($uid, 1, '分享商品，经验值加1。');
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }
    } 

    //分享页面增加积分
    public function actionPageShare() {

        if(!yii::app()->user->isLogged()){
           $this->error('用户未登录'); 
        }
        $page = Yii::app()->request->getParam('page');
        $uid = Yii::app()->user->id;
        $user = MembersModel::model()->findByPk($uid);
        if (empty($user)) {
            $this->error('获取用户信息失败！', self::U('user/login/login'));
        }


        $key = "RedeemgoodController_Share_page_{$uid}";
        $cache = Yii::app()->cache->get($key);
        $time = time();
        $time_today = strtotime('today');

        if(empty($cache)){
            Yii::app()->cache->set($key, $time, $time_today + 86399 - $time);
        }else{
            if($cache > $time_today){ //今天分享过
                $this->error('今天已经分享过此页面');
            }else{//今天没有分享过
                Yii::app()->cache->set($key, $time, $time_today + 86399 - $time);
            }
        }

        //增加积分
        if (MembersModel::model()->plusScore($uid, 10) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享送积分活动，赠送积分 10 。');
            //经验值
            //MembersModel::model()->plusExp($uid, 1, '分享页面，经验值加1。');
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }
    }

    //分享页面增加积分
    public function actionPageShare11() {

        if(!yii::app()->user->isLogged()){
           $this->error('用户未登录'); 
        }
        $page = Yii::app()->request->getParam('page');
        $uid = Yii::app()->user->id;
        $user = MembersModel::model()->findByPk($uid);
        if (empty($user)) {
            $this->error('获取用户信息失败！', self::U('user/login/login'));
        }


        $key = "RedeemgoodController_Share_page11_{$uid}";
        $cache = Yii::app()->cache->get($key);
        $time = time();
        $time_today = strtotime('today');

        if(empty($cache)){
            Yii::app()->cache->set($key, $time, $time_today + 86399 - $time);
        }else{
            if($cache > $time_today){ //今天分享过
                $this->error('今天已经分享过此页面');
            }else{//今天没有分享过
                Yii::app()->cache->set($key, $time, $time_today + 86399 - $time);
            }
        }

        //增加积分
        if (MembersModel::model()->plusScore($uid, 10) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享送积分活动，赠送积分 10 。');
            //经验值
            //MembersModel::model()->plusExp($uid, 1, '分享页面，经验值加1。');
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }
    }      

    //分享积分记录商品
    public function actionRecordgoodShare(){
        if(!yii::app()->user->isLogged()){
           $this->error('用户未登录'); 
        }
        $id = Yii::app()->request->getParam('id');
        $uid = Yii::app()->user->id;
        $user = MembersModel::model()->findByPk($uid);
        if (empty($user)) {
            $this->error('获取用户信息失败！', self::U('user/login/login'));
        }
        $objRedeemRecordsModel = RedeemRecordsModel::model()->findByPk($id);
        if (!$objRedeemRecordsModel) {
            $this->error('找不到记录');
        }
        if($objRedeemRecordsModel->is_share){
            $this->error('已经分享过');
        }

        //增加积分
        if (MembersModel::model()->plusScore($uid, 10) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享积分商品送积分活动，赠送积分 10 。');
            $objRedeemRecordsModel->is_share = 1;
            $objRedeemRecordsModel->save(false,array('is_share'));
            //经验值
            //MembersModel::model()->plusExp($uid, 1, '分享商品，经验值加1。');
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }        
    } 
}
